package com.cool4j.generator.bean;

import com.cool4j.generator.CodeGenerator;
import com.cool4j.generator.util.StringUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 表实体
 *
 * @author Frank create on 16/1/8 20:11
 */
@SuppressWarnings("unused")
public class TableBean {

    /** 数据库 */
    private String schemaName;

    /** 表名 */
    private String name;

    /** 表注释 */
    private String comment;

    /** 列 */
    private List<ColumnBean> columnList = new ArrayList<>();

    /** 包名 */
    private String domainPackage;

    /** 包路径 */
    private String domainPath;

    /** 类名 */
    private String domainClassName;

    /**
     * 获得第一个主键
     *
     * @return - 主键列对象
     */
    public ColumnBean firstPrimaryKey() {
        for (ColumnBean column : columnList) {
            if (column.isPrimary()) {
                return column;
            }
        }
        return null;
    }

    /**
     * 获得首字母小写类名
     *
     * @return - 首字母小写类名
     */
    public String lowerDomainClassName() {
        return StringUtil.firstLower(domainClassName);
    }

    /* --------------------------------------- GETTER / SETTER ---------------------------------- */

    public String getSchemaName() {
        return schemaName;
    }

    public void setSchemaName(String schemaName) {
        this.schemaName = schemaName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public List<ColumnBean> getColumnList() {
        return columnList;
    }

    public void setColumnList(List<ColumnBean> columnList) {
        this.columnList = columnList;
    }

    public String getDomainPackage() {
        return domainPackage;
    }

    public void setDomainPackage(String domainPackage) {
        this.domainPackage = domainPackage;
    }

    public String getDomainPath() {
        if (StringUtils.isBlank(this.domainPath)) {
            this.domainPath = this.getDomainPackage().replace(".", "/");
        }
        return domainPath;
    }

    public void setDomainPath(String domainPath) {
        this.domainPath = domainPath;
    }

    public String getDomainClassName() {
        if ("0".equals(CodeGenerator.flag)) {
            // 直接输出
            if (StringUtils.isBlank(this.domainClassName)) {
                this.domainClassName = StringUtil.firstUpper(StringUtil.underlineToHump(this.name));
            }
        } else if ("1".equals(CodeGenerator.flag)) {
            // 去掉数字模块
            if (StringUtils.isBlank(this.domainClassName)) {
                this.domainClassName = StringUtil.firstUpper(StringUtil.underlineToHump(this.name.substring(this.name.indexOf("_") + 1, this.name.length())));
            }
        }
        return domainClassName;
    }

    public void setDomainClassName(String domainClassName) {
        this.domainClassName = domainClassName;
    }
}
